package com.example.productserver.config;


import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;


@Configuration
public class SentinelConfig {
/*
    @PostConstruct // init-method
    private static void initFlowRules() {
        // 流控规则
        List<FlowRule> rules = new ArrayList<>();
        // 流控
        FlowRule ruleUser = new FlowRule();
        // 设置受保护的资源信息
        ruleUser.setResource("selectProductInfoList");
        //设置流控规则
        *//*
        ruleUser.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // 设置受保护的资源阈值  limit QPS to 1.
        ruleUser.setCount(1);
        *//*
        rules.add(ruleUser);
        // 加载配置好的规则
        FlowRuleManager.loadRules(rules);
    }*/

    /*解决入口资源聚合收敛，导致链路限流不生效的问题*/
    @Bean
    public FilterRegistrationBean sentinelFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CommonFilter());
        registration.addUrlPatterns("/*");
        // 入口资源关闭聚合  解决流控链路不生效的问题
        registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
        registration.setName("sentinelFilter");
        registration.setOrder(1);
        return registration;
    }









}
